@extends('app')

@section('content')
    <div class="layui-card" style="margin-bottom: 0;">
        @include('layouts.breadcrumb')

        <div class="layui-tab-content">
            <form class="layui-form">
                <blockquote class="layui-elem-quote quoteBox" >
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select name="type">
                                <option value="">是否是机器人</option>
                                <option value="1" @if($type==1) selected @endif>否</option>
                                <option value="2" @if($type==2) selected @endif>是</option>
                            </select>
                        </div>
                    </div>
                    <a class="layui-btn search_btn" data-type="reload" lay-submit lay-filter="search" style="margin-left: 3px"><i class="layui-icon layui-icon-search"></i>查询</a>
                </blockquote>
            </form>
            <div class="layui-tab-item layui-show">
                <table class="layui-hide" id="model" lay-filter="model"></table>
            </div>
        </div>
    </div>

    {{--表头操作--}}
    <script type="text/html" id="upload">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="edit">新增</button>
        </div>
    </script>

    {{--表列操作--}}
    <script type="text/html" id="operate">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
    </script>
@endsection

@section('js')
    <script>
        layui.config({
            version:false
        }).use('table', function(){
            var table = layui.table;
            var form = layui.form;
            var $   = layui.$;

            form.on('submit(search)', function(data){
                var field = data.field;
                //执行重载
                table.reload('model', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    },
                    where: field
                });
            });

            table.render({
                elem: '#model',
                url:'/user/index',
                cellMinWidth: 95, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                height: 'full-190',
                method : 'POST',
                page: true, //开启分页
                defaultToolbar: ['filter', 'print', 'exports'],
                toolbar:'#upload',
                limit:20,
                loading:true,
                rowStyle: function (index, item) {//index第几行，row当前行的值
                    return 'data-id="'+item.id+'" ';
                },
                cols: [[
                    {type: 'checkbox', fixed: 'left'},
                    {field:'id', title: 'ID',align:'center'},
                    {field:'type', title: '机器人',align:'center',templet:function(item){return item.type==1?'否':'是'}},
                    {field:'avatar',title: '头像',templet:function(item){
                            return '<img src="'+item.avatar+'" height="100%"/>';
                        }
                    },
                    {field:'phone',width:'180',title: '手机号'},
                    {field:'username',width:'180',title: '昵称'},
                    {field:'sex',width:'80',title: '性别',templet:function(item){return item.sex==1?'男':'女';}},
                    {field:'article_count',width:'180',title: '发帖量'},
                    {field:'comment_count',width:'180',title: '评论量'},
                    {title: '操作',width:200, align:'center', fixed: 'right', toolbar: '#operate'}
                ]]
            });

            //表头操作
            table.on('toolbar(model)', function(obj){
                switch(obj.event){
                    case 'edit':
                        newWindow('添加用户', '/user/edit','500','500',true);
                        break;
                };
            });

            //列操作
            table.on('tool(model)',function (obj) {
                if(obj.event === 'del'){//删除行
                    del(obj);
                }else if(obj.event==="edit"){
                    var data = obj.data;
                    newWindow('编辑','/user/edit?id='+data.id,500,500,true);
                }
            });

            function del(data) {

                layer.confirm("你确定删除这个用户么?", {icon: 3, title:'提示'},
                    function(index){//确定回调
                        var id = data.data.id;
                        console.info(id);
                        $.ajax({
                            url:'/user/del',
                            data:{id:id},
                            success:function(data){
                                layer.alert(data.msg,{icon: 1},function () {
                                    location.reload('model');
                                    parent.layer.closeAll();
                                });
                            }
                        })
                    },function (index) {//取消回调
                        layer.close(index);
                    }
                );
            }

        })
    </script>
@endsection